Component({
  properties: {
    // 有奶茶记录的日期，格式如 ['2023/5/15', '2023/5/18']
    spot: {
      type: Array,
      value: []
    }
  },

  data: {
    year: new Date().getFullYear(),
    month: new Date().getMonth() + 1, // 月份从1开始
    weekdays: ['日', '一', '二', '三', '四', '五', '六'],
    prevMonthDays: [], // 上个月显示的天数
    currentMonthDays: [], // 当前月的天数
    nextMonthDays: [], // 下个月显示的天数
    today: {
      year: new Date().getFullYear(),
      month: new Date().getMonth() + 1,
      day: new Date().getDate()
    },
    selectedDay: new Date().getDate(), // 当前选中的日期
    prevYear: 0, // 上个月的年份
    prevMonth: 0, // 上个月
    nextYear: 0, // 下个月的年份
    nextMonth: 0 // 下个月
  },

  lifetimes: {
    attached() {
      this.calculateDays();
    }
  },

  methods: {
    // 计算日历上需要显示的日期
    calculateDays() {
      const { year, month } = this.data;
      
      // 计算当前月的天数
      const currentMonthDays = new Date(year, month, 0).getDate();
      
      // 计算当前月第一天是星期几
      const firstDayOfMonth = new Date(year, month - 1, 1).getDay();
      
      // 计算上个月的年和月
      let prevYear = year;
      let prevMonth = month - 1;
      if (prevMonth === 0) {
        prevYear -= 1;
        prevMonth = 12;
      }
      
      // 计算下个月的年和月
      let nextYear = year;
      let nextMonth = month + 1;
      if (nextMonth === 13) {
        nextYear += 1;
        nextMonth = 1;
      }
      
      // 计算上个月的天数
      const prevMonthDays = new Date(prevYear, prevMonth, 0).getDate();
      
      // 显示在当前月日历上的上个月的日期
      const prevDays = [];
      for (let i = 0; i < firstDayOfMonth; i++) {
        prevDays.unshift(prevMonthDays - i);
      }
      
      // 当前月的日期
      const currentDays = [];
      for (let i = 1; i <= currentMonthDays; i++) {
        currentDays.push(i);
      }
      
      // 计算下个月需要显示的天数
      const totalDaysShown = 42; // 6行7列
      const nextDays = [];
      const remainingDays = totalDaysShown - prevDays.length - currentDays.length;
      for (let i = 1; i <= remainingDays; i++) {
        nextDays.push(i);
      }
      
      this.setData({
        prevMonthDays: prevDays,
        currentMonthDays: currentDays,
        nextMonthDays: nextDays,
        prevYear,
        prevMonth,
        nextYear,
        nextMonth
      });
    },
    
    // 检查某个日期是否有记录
    hasRecord(year, month, day) {
      const { spot } = this.properties;
      const formattedDate = `${year}/${month}/${day}`;
      return spot.includes(formattedDate);
    },
    
    // 选择日期
    selectDate(e) {
      const date = e.currentTarget.dataset.date;
      const parts = date.split('/');
      const year = parseInt(parts[0]);
      const month = parseInt(parts[1]);
      const day = parseInt(parts[2]);
      
      // 如果点击的是当前月的日期，更新选中的日期
      if (year === this.data.year && month === this.data.month) {
        this.setData({
          selectedDay: day
        });
      } else {
        // 如果点击的是其他月份的日期，切换到该月份
        this.setData({
          year,
          month,
          selectedDay: day
        }, () => {
          this.calculateDays();
        });
      }
      
      // 触发日期点击事件
      this.triggerEvent('dayClick', { date });
    },
    
    // 切换到上个月
    prevMonth() {
      let { year, month } = this.data;
      month--;
      if (month === 0) {
        year--;
        month = 12;
      }
      
      this.setData({
        year,
        month,
        selectedDay: null
      }, () => {
        this.calculateDays();
      });
    },
    
    // 切换到下个月
    nextMonth() {
      let { year, month } = this.data;
      month++;
      if (month === 13) {
        year++;
        month = 1;
      }
      
      this.setData({
        year,
        month,
        selectedDay: null
      }, () => {
        this.calculateDays();
      });
    }
  }
}) 